package com.shycloud.mido.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.MemberRole;
import com.shycloud.mido.member.api.entity.MiniBanner;
import com.shycloud.mido.member.api.entity.MiniRewardRecord;
import com.shycloud.mido.member.api.util.Constant;
import com.shycloud.mido.member.mapper.MemberMapper;
import com.shycloud.mido.member.mapper.MemberRoleMapper;
import com.shycloud.mido.member.mapper.MiniBannerMapper;
import com.shycloud.mido.member.mapper.MiniRewardRecordMapper;
import com.shycloud.mido.member.service.MiniRewardRecordService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 小程序奖励领取表 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2021-06-18
 */
@Service
@AllArgsConstructor
public class MiniRewardRecordServiceImpl extends
		ServiceImpl<MiniRewardRecordMapper, MiniRewardRecord> implements MiniRewardRecordService {

	/** 领取奖励 mapper */
	private final MiniRewardRecordMapper rewardRecordMapper;
	/** 首页 banner mapper */
	private final MiniBannerMapper bannerMapper;
	/** 用户模块 mapper */
	private final MemberMapper memberMapper;
	/** VIP模块 mapper */
	private final MemberRoleMapper memberRoleMapper;

	@Override
	public boolean receive(String bannerId) {
		//获取用户ID
		String memberId = SecurityUtils.getMember().getId();
		//判断是否领取过奖励
		int count = rewardRecordMapper.selectCount(new QueryWrapper<MiniRewardRecord>()
				.eq("member_id", memberId)
				.eq("banner_id", bannerId));
		if (count > 0) {
			throw new BusinessException("您已领取过奖励，无法重复领取");
		}

		//执行领取逻辑 ==============>
		MiniBanner miniBanner = bannerMapper.selectById(bannerId);
		if (miniBanner == null) {
			throw new BusinessException("所传Id查询不到对应 banner");
		}
		//领取快乐贝
		if (MiniBanner.CLICKRE_WARD_TYPE_GOLD.equals(miniBanner.getClickRewardType())) {
			Member member = memberMapper.selectById(memberId);
			member.setGold(member.getGold() + miniBanner.getDurationTypeValue());
			memberMapper.updateById(member);
		}
		//领取 VIP
		if (MiniBanner.CLICKRE_WARD_TYPE_VIP.equals(miniBanner.getClickRewardType())) {

			int duration = 0;

			//计算开通时长
			if (MiniBanner.DURATION_TYPE_YEAR.equals(miniBanner.getDurationType())) {
				//年
				duration = miniBanner.getDurationTypeValue() * 365;
			}
			if (MiniBanner.DURATION_TYPE_MON.equals(miniBanner.getDurationType())) {
				//月
				duration = miniBanner.getDurationTypeValue() * 30;
			}
			if (MiniBanner.DURATION_TYPE_DAY.equals(miniBanner.getDurationType())) {
				//日
				duration = miniBanner.getDurationTypeValue();
			}

			// 会员时长累加
			MemberRole role = memberRoleMapper.selectOne(new QueryWrapper<MemberRole>()
					.eq("member_id", memberId)
					.and(x -> x.eq("role_code", Constant.VIP_MEMBER)));

			if (role != null) {

				//创建时间
				role.setCreateTime(LocalDateTime.now());
				//如果还没到期，那么从到期时间开始累加
				if (role.getEndTime().isAfter(LocalDateTime.now())) {
					role.setEndTime(role.getEndTime().plusDays(duration));
				}
				//如果已经到期，那么从今天开始累加会员天数
				else {
					role.setStartTime(LocalDateTime.now());
					role.setEndTime(LocalDateTime.now().plusDays(duration));
				}
				memberRoleMapper.updMemberRole(role);

			} else {
				//新增会员
				role = new MemberRole();
				//创建时间
				role.setCreateTime(LocalDateTime.now());
				//开始时间
				role.setStartTime(LocalDateTime.now());
				//结束时间
				role.setEndTime(LocalDateTime.now().plusDays(duration));
				//会员
				role.setRoleCode(Constant.VIP_MEMBER);
				//用户ID
				role.setMemberId(memberId);
				//逻辑删除
				role.setDelFlag(Constant.DEL_FLAG);
				memberRoleMapper.saveMemberRole(role);
			}
		}
		//领取完毕 <======================
		//添加领取记录信息
		MiniRewardRecord miniRewardRecord = new MiniRewardRecord();
		//banner Id
		miniRewardRecord.setBannerId(bannerId);
		//用户ID
		miniRewardRecord.setMemberId(memberId);
		//领取时间
		miniRewardRecord.setCreateTime(LocalDateTime.now());
		miniRewardRecord.setCreateUser(memberId);

		return rewardRecordMapper.insert(miniRewardRecord) == 1;
	}

	@Override
	public boolean checkReceive(String bannerId) {
		//获取用户ID
		String memberId = SecurityUtils.getMember().getId();
		//判断是否领取过奖励
		int count = rewardRecordMapper.selectCount(new QueryWrapper<MiniRewardRecord>()
				.eq("member_id", memberId)
				.eq("banner_id", bannerId));
		if (count > 0) {
			return true;
		}
		return false;
	}
}
